home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.2 Applications 1996 May / SGI IRIX 6.2 Applications 1996 May.iso / dist / impr_dev.idb / usr / impressario / src / examples / libprintui / printbox.c.z / printbox.c
Encoding:
C/C++ Source or Header  |  1996-05-07  |  12.0 KB  |  454 lines

  1. /**************************************************************************
  2.  *                                      *
  3.  *           Copyright (c) 1991 Silicon Graphics, Inc.              *
  4.  *            All Rights Reserved                    *
  5.  *                                      *
  6.  *         THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI             *
  7.  *                                      *
  8.  * The copyright notice above does not evidence any actual of intended    *
  9.  * publication of such source code, and is an unpublished work by Silicon *
  10.  * Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is *
  11.  * the property of Silicon Graphics, Inc. Any use, duplication or         *
  12.  * disclosure not specifically authorized by Silicon Graphics is strictly *
  13.  * prohibited.                                  *
  14.  *                                      *
  15.  * RESTRICTED RIGHTS LEGEND:                          *
  16.  *                                      *
  17.  * Use, duplication or disclosure by the Government is subject to         *
  18.  * restrictions as set forth in subdivision (c)(1)(ii) of the Rights in   *
  19.  * Technical Data and Computer Software clause at DFARS 52.227-7013,      *
  20.  * and/or in similar or successor clauses in the FAR, DOD or NASA FAR     *
  21.  * Supplement. Unpublished - rights reserved under the Copyright Laws of  *
  22.  * the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.       *
  23.  * Shoreline Blvd., Mountain View, CA 94039-7311                          *
  24.  **************************************************************************
  25.  *
  26.  * File: printbox.c
  27.  *
  28.  * Description: Simply instantiates a PrintBox widget.
  29.  *
  30.  **************************************************************************/
  31.  
  32.  
  33. #ident "$Revision: 1.8 $"
  34.  
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <sys/types.h>
  41. #include <Xm/Xm.h>
  42. #include <X11/StringDefs.h>
  43. #include <Xm/MessageB.h>
  44. #include <Sgm/PrintBox.h>
  45.  
  46.  
  47. /* Program globals */
  48.  
  49. static char *prog_name;            /* Program instance name (no path) */
  50. static char *prog_class = "Printbox";    /* Program class name */
  51. static Widget top_level;        /* Top level widget */
  52. static Widget print_box;        /* printBox widget */
  53. static Widget error_dialog;        /* Error dialog */
  54. static XtAppContext app_context;    /* Application context */
  55.  
  56.  
  57. /* Replacement error messages */
  58.  
  59. static char *no_spooler_mess[] = {
  60.     "The print scheduler /usr/lib/lpsched is not running.",
  61.     "Contact your system administrator."
  62.     };
  63. static int no_spooler_mess_lines = sizeof(no_spooler_mess) / sizeof(char*);
  64.  
  65.  
  66. /* Help message */
  67.  
  68. static char *help_mess[] = {
  69.     "'printbox' simply instantiates a PrintBox widget."
  70.     };
  71. static int help_mess_lines = sizeof(help_mess) / sizeof(char*);
  72.  
  73. /* Local functions */
  74.  
  75. static void cancel_cb(Widget, XtPointer, XtPointer);
  76. static void print_cb(Widget, XtPointer, XtPointer);
  77. static void error_cb(Widget, XtPointer, XtPointer);
  78. static void help_cb(Widget, XtPointer, XtPointer);
  79. static Widget create_error_dialog(void);
  80. static XmString create_message(char**, int);
  81. static void manage_error_cb(Widget, XtPointer, XEvent*, Boolean*);
  82.  
  83.  
  84. /**************************************************************************
  85.  *
  86.  * Function: main
  87.  *
  88.  * Description: Program entry point
  89.  *
  90.  * Parameters: 
  91.  *    argc (I) - command line argument count
  92.  *    argv (I) - command line arguments
  93.  *
  94.  * Return: None
  95.  *
  96.  **************************************************************************/
  97.  
  98. int main(int argc, char *argv[])
  99. {
  100.     char *str;
  101.  
  102.     /*
  103.      * Set program instance name
  104.      */
  105.     str = strrchr(*argv, '/');
  106.     prog_name = strdup((str == NULL) ? argv[0]: str + 1);
  107.  
  108.     /*
  109.      * Initialize the X app connection.
  110.      */
  111.     top_level = XtAppInitialize(&app_context, prog_class,
  112.                 NULL, 0, &argc, argv, NULL, NULL, 0);
  113.  
  114.     /*
  115.      * Create the PrintBox widget
  116.      */
  117.     print_box = PuiCreatePrintBox(top_level, "printBox", NULL, 0);
  118.     XtManageChild(print_box);
  119.  
  120.     /*
  121.      * Add callbacks
  122.      */
  123.     XtAddCallback(print_box, PuiNcancelCallback, cancel_cb, NULL);
  124.     XtAddCallback(print_box, PuiNhelpCallback, help_cb, NULL);
  125.     XtAddCallback(print_box, PuiNjobInfoCallback, print_cb, NULL);
  126.     XtAddCallback(print_box, PuiNerrorCallback, error_cb, NULL);
  127.     XtAddCallback(print_box, PuiNoptionErrorCallback, error_cb, NULL);
  128.  
  129.     /*
  130.      * Realize all widgets
  131.      */
  132.     XtRealizeWidget(top_level);
  133.  
  134.     /*
  135.      * Start processing events.
  136.      */
  137.     XtAppMainLoop(app_context);
  138.  
  139.     /* NOTREACHED */
  140.     return(1);
  141. }
  142.  
  143.  
  144. /*=========================================================================
  145.                 LOCAL FUNCTIONS
  146.   =========================================================================*/
  147.  
  148.  
  149. /**************************************************************************
  150.  *
  151.  * Function: cancel_cb
  152.  *
  153.  * Description: Cancel button exit routine.
  154.  *
  155.  * Parameters: 
  156.  *    w (I) - invoking widget
  157.  *    client_data (I) - client data
  158.  *    call_data (I) - standard callback info
  159.  *
  160.  * Return: none
  161.  *
  162.  **************************************************************************/
  163. /* ARGSUSED */
  164.  
  165. static void cancel_cb(Widget w, XtPointer client_data, XtPointer call_data)
  166. {
  167.     exit(0);
  168. }
  169.  
  170.  
  171. /**************************************************************************
  172.  *
  173.  * Function: print_cb
  174.  *
  175.  * Description: Print job information callback. This callback is called
  176.  *    if a print job has been successfully submitted. The job ID is
  177.  *    printed to standard out and the program terminates.
  178.  *
  179.  * Parameters: 
  180.  *    w (I) - invoking widget
  181.  *    client_data (I) - client data
  182.  *    call_data (I) - standard callback info
  183.  *
  184.  * Return: none
  185.  *
  186.  **************************************************************************/
  187. /* ARGSUSED */
  188.  
  189. static void print_cb(Widget w, XtPointer client_data, XtPointer call_data)
  190. {
  191.     PuiPrintBoxCallbackStruct *pb_cb = (PuiPrintBoxCallbackStruct*)call_data;
  192.  
  193.     /*
  194.      * Print job ID
  195.      */
  196.     (void)printf("request id is %s\n", pb_cb->job_info->job_id);
  197.     
  198.     exit(0);
  199. }
  200.  
  201.  
  202. /**************************************************************************
  203.  *
  204.  * Function: error_cb
  205.  *
  206.  * Description: Handles the display of error messages from the printBox
  207.  *    widget.
  208.  *
  209.  * Parameters: 
  210.  *    w (I) - invoking widget
  211.  *    client_data (I) - client data
  212.  *    call_data (I) - standard callback info
  213.  *
  214.  * Return: none
  215.  *
  216.  **************************************************************************/
  217. /* ARGSUSED */
  218.  
  219. static void error_cb(Widget w, XtPointer client_data, XtPointer call_data)
  220. {
  221.     XmString str;
  222.     char **err_buf, *estr;
  223.     int num_str;
  224.     PuiPrintBoxCallbackStruct *pb_cb = (PuiPrintBoxCallbackStruct*)call_data;
  225.     Arg arg;
  226.  
  227.     /*
  228.      * If first time, create the error dialog window
  229.      */
  230.     if (!error_dialog)
  231.     error_dialog = create_error_dialog();
  232.  
  233.     /*
  234.      * Determine if this is an option panel error or printbox error
  235.      */
  236.     if (pb_cb->reason == PuiCR_OPT_ERROR) {
  237.         estr = strerror(pb_cb->error_code);
  238.         err_buf = &estr;
  239.         num_str = 1;
  240.     } else {
  241.         /*
  242.          * Select the appropriate message
  243.          */
  244.         switch (pb_cb->error_code) {
  245.         case SL_ERR_SPOOLER_ERROR:
  246.             SLGetSpoolerError(&err_buf, &num_str);
  247.             break;
  248.         case SL_ERR_SPOOLER_UNKNOWN:
  249.             XtAddCallback(error_dialog, XmNokCallback, cancel_cb, NULL);
  250.             err_buf = no_spooler_mess;
  251.             num_str = no_spooler_mess_lines;
  252.             break;
  253.         default:
  254.             estr = SLErrorString(pb_cb->error_code);
  255.             err_buf = &estr;
  256.             num_str = 1;
  257.             break;
  258.         }
  259.     }
  260.  
  261.     /*
  262.      * Set the message in the error dialog
  263.      */
  264.     str = create_message(err_buf, num_str);
  265.     XtSetArg(arg, XmNmessageString, str);
  266.     XtSetValues(error_dialog, &arg, 1);
  267.     XmStringFree(str);
  268.  
  269.     /*
  270.      * Bring up the dialog
  271.      */
  272.     if (XtIsRealized(top_level))
  273.         XtManageChild(error_dialog);
  274.     else
  275.     XtAddEventHandler(top_level, VisibilityChangeMask, False,
  276.                         manage_error_cb, NULL);
  277. }
  278.  
  279.  
  280. /**************************************************************************
  281.  *
  282.  * Function: manage_error_cb
  283.  *
  284.  * Description: Handles the delayed display of an error dialog. The
  285.  *    delay was due to the PrintBox not being visible when the error
  286.  *    occurred. We want it visible so that the error dialog is displayed
  287.  *    in an appropriate place on the program window.
  288.  *
  289.  * Parameters:
  290.  *      w (I) - top level widget
  291.  *      client_data (I) - not used
  292.  *      event (I) - event that triggered the call
  293.  *      cont (I) - continue propagating event flag
  294.  *
  295.  * Return: none
  296.  *
  297.  **************************************************************************/
  298.  
  299. static void manage_error_cb(Widget w, XtPointer client_data, XEvent *event,
  300.                                                                 Boolean *cont)
  301. {
  302.     XtRemoveEventHandler(top_level, VisibilityChangeMask, False,
  303.                         manage_error_cb, NULL);
  304.     XtManageChild(error_dialog);
  305. }
  306.  
  307.  
  308. /**************************************************************************
  309.  *
  310.  * Function: help_cb
  311.  *
  312.  * Description: Displays the help message.
  313.  *
  314.  * Parameters: 
  315.  *    w (I) - invoking widget
  316.  *    client_data (I) - not used
  317.  *    call_data (I) - not used
  318.  *
  319.  * Return: none
  320.  *
  321.  **************************************************************************/
  322. /* ARGSUSED */
  323.  
  324. static void help_cb(Widget w, XtPointer client_data, XtPointer call_data)
  325. {
  326.     static Widget help_dialog = NULL;
  327.  
  328.     /*
  329.      * If first time, create the help dialog
  330.      */
  331.     if (!help_dialog) {
  332.         Arg wargs[5];
  333.         XmString tstr, mstr;
  334.         register int n;
  335.  
  336.         /*
  337.          * Create title and message strings
  338.          */
  339.         tstr = XmStringCreateSimple("Help");
  340.     mstr = create_message(help_mess, help_mess_lines);
  341.  
  342.         /*
  343.          * Create a dialog
  344.          */
  345.         n = 0;
  346.         XtSetArg(wargs[n], XmNdialogTitle, tstr); n++;
  347.         XtSetArg(wargs[n], XmNmessageString, mstr); n++;
  348.         help_dialog = XmCreateInformationDialog(top_level, "helpDialog",
  349.                                 wargs, n);
  350.  
  351.         /*
  352.          * Blow off Help and Cancel
  353.          */
  354.         XtUnmanageChild(XmMessageBoxGetChild(help_dialog,
  355.                         XmDIALOG_CANCEL_BUTTON));
  356.         XtUnmanageChild(XmMessageBoxGetChild(help_dialog,
  357.                         XmDIALOG_HELP_BUTTON));
  358.  
  359.         XmStringFree(tstr);
  360.         XmStringFree(mstr);
  361.     }
  362.  
  363.     /*
  364.      * Display the dialog
  365.      */
  366.     XtManageChild(help_dialog);
  367. }
  368.  
  369.  
  370. /**************************************************************************
  371.  *
  372.  * Function: create_error_dialog
  373.  *
  374.  * Description: Creates the error dialog 
  375.  *
  376.  * Parameters: none
  377.  *
  378.  * Return: Created dialog widget
  379.  *
  380.  **************************************************************************/
  381.  
  382. static Widget create_error_dialog()
  383. {
  384.     Widget dialog;
  385.     Arg wargs[5];
  386.     XmString tstr;
  387.     register int n;
  388.  
  389.     /*
  390.      * Create some compound strings
  391.      */
  392.     tstr = XmStringCreateSimple("Error");    /* Dialog window title */
  393.  
  394.     /*
  395.      * Create a modal dialog
  396.      */
  397.     n = 0;
  398.     XtSetArg(wargs[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++;
  399.     XtSetArg(wargs[n], XmNdialogTitle, tstr); n++;
  400.     dialog = XmCreateErrorDialog(top_level, "errorDialog", wargs, n);
  401.  
  402.     /*
  403.      * Blow off Help and Cancel
  404.      */
  405.     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
  406.     XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
  407.  
  408.     XmStringFree(tstr);
  409.     
  410.     return dialog;
  411. }
  412.  
  413.  
  414. /**************************************************************************
  415.  *
  416.  * Function: create_message
  417.  *
  418.  * Description: A utility function to create a compound string message from
  419.  *      a multi-element array of strings.
  420.  *
  421.  * Parameters:
  422.  *      str_array (I) - array of message strings.
  423.  *    num_str (I) - number of strings in array.
  424.  *
  425.  * Return: A compound string.
  426.  *
  427.  **************************************************************************/
  428.  
  429. static XmString create_message(char **str_array, int num_str)
  430. {
  431.     XmString xm_newstr, xtemp, xmstr;
  432.     register char *str;
  433.     register int i;
  434.  
  435.     xmstr = XmStringCreateSimple("");
  436.     for (i = 0, str = *str_array; i < num_str; str = *++str_array, i++) {
  437.     if (i) {
  438.             xm_newstr = XmStringSeparatorCreate();
  439.             xtemp = xmstr;
  440.         xmstr = XmStringConcat(xtemp, xm_newstr);
  441.         XmStringFree(xtemp);
  442.         XmStringFree(xm_newstr);
  443.     }
  444.     xm_newstr = XmStringCreateSimple(str);
  445.     xtemp = xmstr;
  446.     xmstr = XmStringConcat(xtemp, xm_newstr);
  447.     XmStringFree(xtemp);
  448.     XmStringFree(xm_newstr);
  449.     }
  450.  
  451.     return xmstr;
  452. }
  453.  
  454.